package com.hwd.gmall.seckill.listener;

import com.hwd.gmall.common.aspect.ListenMessage;
import com.hwd.gmall.common.constant.SeckillConst;
import com.hwd.gmall.seckill.service.SeckillOrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 监听秒杀下单的消息消费者
 *
 * @author 黄伟东/Victor
 * @date 2022/5/12 23:08
 */
@Component
@Log4j2
public class SeckillOrderTimeOutListener {

    @Resource
    private SeckillOrderService seckillOrderService;


    /**
     * 监听延迟消息，取消超市订单
     *
     * @param message 消息体
     * @param ignoredChannel 连接
     */
    @RabbitListener(queues = SeckillConst.SECKILL_GOODS_NORMAL_QUEUE)
    @ListenMessage(messageDescription = "秒杀订单超时的消息")
    public void seckillOrderTimeout(Message message, Channel ignoredChannel) {
        // 取消超时订单
        seckillOrderService.cancelSeckillOrder(new String(message.getBody()), null);
    }
}
